package peer.p2pnet.message;

import java.net.DatagramPacket;

import peer.Config;
import peer.Message;
import peer.tools.ByteIntSwitch;
import peer.tools.ByteShortSwitch;

public class RegMessage extends Message{
	public short sType=peer.tools.MessageType.sType_RegMessage;
	public short nodeType=0;
	byte[] dpData=new byte[18];
	int DBServerPort;
	int AssistServerPort;
	int routePort;
	public RegMessage(){
		routePort=peer.Config.RoutePort;
		ByteShortSwitch.toByteArray(ver, dpData, 0);  //add: ver
		ByteShortSwitch.toByteArray(sType, dpData, 2);  //add: service type
		ByteShortSwitch.toByteArray(Config.myNodeType, dpData, 4);  //add: my node type
		ByteIntSwitch.toByteArray(routePort, dpData, 6);
		ByteIntSwitch.toByteArray(Config.DBServerPort, dpData, 10);
		ByteIntSwitch.toByteArray(Config.AssistServerPort, dpData, 14);
		dp=new DatagramPacket(dpData,dpData.length);
	}
	
	public RegMessage(DatagramPacket dp){
		this.dp=dp;
		dpData=dp.getData();
		ver=ByteShortSwitch.toShort(dpData, 0);
		sType=ByteShortSwitch.toShort(dpData, 2);
		nodeType=ByteShortSwitch.toShort(dpData, 4);
		routePort=ByteIntSwitch.toInt(dpData, 6);
		DBServerPort=ByteIntSwitch.toInt(dpData, 10);
		AssistServerPort=ByteIntSwitch.toInt(dpData, 14);
	}
	
	public short getNodeType(){
		return nodeType;
	}
	
	public int getDBServerPort(){
		return DBServerPort;
	}
	
	public int getAssistServerPort(){
		return AssistServerPort;
	}
	
	public int getRoutePort(){
		return routePort;
	}
}
